﻿using LineFocus.Nikcron.Models;
using Nickron.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LineFocus.Nikcron.Controllers
{
    public class ServiceController : BaseController
    {
        //
        // GET: /Service/
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult ServiceMaintenance()
        {
            return View();
        }

        [HttpGet]
        public ActionResult ServiceCentres()
        {
            ViewBag.Header = "Service Centres";
            ViewBag.Caption = "Listing";
            ViewBag.Module = "Home";
            ApplicationDBContext db = new ApplicationDBContext();
            Company company = new Company();
            return View();
        }

        [HttpGet]
        public JsonResult GetServiceCentres()
        {
            ApplicationDBContext db = new ApplicationDBContext();
            var serviceCentres = from c in db.Offices.OfType<ServiceCentre>().ToList().Where(c => c.Status.IsDeleted != true)
                            select new
                            {
                                Id = c.Id,
                                Name = c.Name,
                                ContactPerson = c.ContactPerson,
                                AddressLine1 = c.Address.Address1,
                                AddressLine2 = c.Address.Address2,
                                AddressCity = c.Address.City,
                                AddressState = c.Address.State,
                                AddressZip = c.Address.Zip,
                                Mobile = string.Join(" ", c.Contact1.Mobile, c.Contact2.Mobile),
                                Phone = c.Contact1.Phone,
                                Email = c.Contact1.Email,
                                Web = c.Contact1.Website
                            };
            JsonResult result = new JsonResult();
            result.Data = serviceCentres;
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return result;
        }

        [HttpGet]
        public ActionResult ServiceCentreMaintenance(Int32? Id)
        {
            ViewBag.Header = "Service Centres";
            Lookup Lookup_States = new Lookup();
            Lookup Lookup_Countries = new Lookup();
            ApplicationDBContext db = new ApplicationDBContext();
            var query1 = from records in db.States
                         select new LookupItem() { Id = records.Id, Name = records.Name };
            Lookup_States.Items = query1.ToList();

            var query2 = from records in db.Countries
                         select new LookupItem() { Id = records.Id, Name = records.Name };
            Lookup_Countries.Items = query2.ToList();
            ViewBag.States = Lookup_States;
            ViewBag.Countries = Lookup_Countries;
            if (Id.HasValue)
            {
                ViewBag.Caption = "Edit";
                ViewBag.Id = Id;

                ServiceCentre ServiceCentre = db.Offices.OfType<ServiceCentre>().Where(c => c.Id == Id).ToList().FirstOrDefault();
                return View(ServiceCentre);
            }
            else
            {
                ViewBag.Caption = "New";
                return View(new ServiceCentre());
            }
        }

        [HttpPost]
        public ActionResult ServiceCentreMaintenance(FormCollection formCollection)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            ServiceCentre ServiceCentre = new ServiceCentre();
            User User = new Nickron.Database.User();

            Int32 ServiceCentreId = 0;
            Int32.TryParse(formCollection["Id"], out ServiceCentreId);
            if (ServiceCentreId == 0)
                ServiceCentre = new ServiceCentre();
            else
                ServiceCentre = db.Offices.OfType<ServiceCentre>().Where(c => c.Id == ServiceCentreId).FirstOrDefault();

            ServiceCentre.Name = formCollection["Name"];
            ServiceCentre.ContactPerson = formCollection["ContactPerson"];
            ServiceCentre.Address.Address1 = formCollection["Address1"];
            ServiceCentre.Address.Address2 = formCollection["Address2"];
            ServiceCentre.Address.City = formCollection["City"];
            ServiceCentre.Address.Zip = formCollection["Zip"];
            ServiceCentre.Address.State = formCollection["State"];
            ServiceCentre.Address.Country = formCollection["Country"];
            ServiceCentre.Contact1.Mobile = formCollection["Mobile1"];
            ServiceCentre.Contact2.Mobile = formCollection["Mobile2"];
            ServiceCentre.Contact1.Phone = formCollection["Phone1"];
            ServiceCentre.Contact1.Email = formCollection["Email"];
            ServiceCentre.Contact1.Website = formCollection["Website"];

            // Company. = formCollection["Username"];
            // Company. = formCollection["Password"];

            if (ServiceCentreId == 0)
                db.Offices.AddObject(ServiceCentre);
            db.SaveChanges();

            if (ServiceCentreId == 0)
            {
                AddNewUser(ServiceCentre, Common.OfficeTypes.ServiceCentre);
            }
            return RedirectToAction("Index");
        }
    }
}
